package jh.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.List;

import jh.interfaces.OnItemPictureClickListener;
import jh.utils.Utils;

/**
 * 一般项目就实现NineGridLayout类即可，如果没有特殊需求，不要改动NineGridLayout类
 */
public class NineGridTestLayout extends NineGridLayout {

    private Context context;
    private int itemPosition;
    private OnItemPictureClickListener listener;

    public NineGridTestLayout(Context context) {
        this(context,null);
    }

    public NineGridTestLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    /**
     * 优化picasso加载图片 避免oom resize(500,500).centerCrop() 裁剪
     * .config(Bitmap.Config.RGB_565)
     */
    @Override
    protected void displayImage(int position,RatioImageView imageView, String url) {
        Log.e("wefwefwefwefwefwf","url===="+url);
        if(context!=null&& !TextUtils.isEmpty(url)&&!url.equals("")){
            if(url.endsWith("jpg")||url.endsWith("png")){
                Picasso.with(context).load(url).config(Bitmap.Config.RGB_565).resize(500,500).centerCrop().into(imageView);
                imageView.setTag(Utils.getNameByPosition(itemPosition,position));
                imageView.setTransitionName(Utils.getNameByPosition(itemPosition,position));
            }
        }
    }

    @Override
    protected void onClickImage(int imageIndex, String url, List<String> urlList, ImageView imageView) {
        listener.onItemPictureClick(itemPosition,imageIndex,url,urlList,imageView);
    }


    public void setItemPosition(int itemPosition) {
        this.itemPosition = itemPosition;
    }

    public void setListener(OnItemPictureClickListener listener) {
        this.listener = listener;
    }
}
